package com.yzk.auction.dao;

import com.yzk.auction.entity.OrderAll;
import com.yzk.auction.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDao {

    /**
     * 显示全部订单
     * */
    public static List<OrderAll> query(){
        DataSource dataSource = JDBCUtils.getDataSource();
        QueryRunner qr = new QueryRunner(dataSource);
        List<OrderAll> list = null ;
        try {
            list = qr.query("select order_id,goods_name,user_name,order_time,pay_status,user_addr \n" +
                            "from tb_goods,tb_users,tb_orders \n" +
                            "where tb_goods.goods_id=tb_orders.goods_id and tb_orders.user_id=tb_users.user_id;",
                    new BeanListHandler<>(OrderAll.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 订单增，删，改
     * */
    public static int sql_update(String sql){
        int i = 0;
        Connection conn=null;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            QueryRunner qr = new QueryRunner();
            i = qr.update(conn,sql);
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if(conn!=null && !conn.isClosed()){
                    conn.rollback();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return i;
    }
}
